package com.taiji.web.controller.workOrder;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.taiji.common.core.domain.entity.SysRole;
import com.taiji.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.taiji.common.annotation.Log;
import com.taiji.common.core.controller.BaseController;
import com.taiji.common.core.domain.AjaxResult;
import com.taiji.common.enums.BusinessType;
import com.taiji.workOrder.domain.Message;
import com.taiji.workOrder.service.IMessageService;
import com.taiji.common.utils.poi.ExcelUtil;
import com.taiji.common.core.page.TableDataInfo;

/**
 * 工单信息Controller
 * 
 * @author taiji
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/workOrder/message")
@Api(tags = "工单消息")
public class MessageController2 extends BaseController
{
    @Autowired
    private IMessageService messageService;

    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:message:list')")
    @GetMapping("/list")
    @ApiOperation("查询工单信息列表")
    public TableDataInfo list(Message message)
    {
        List<SysRole> userRoles = SecurityUtils.getLoginUser().getUser().getRoles();

        List<String> roleKeys = userRoles.stream()
                .map(SysRole::getRoleKey)
                .collect(Collectors.toList());

        for (String roleKey : roleKeys) {
            // 如果权限中包含业务用户
            if (roleKey.contains("bus")) {
                message.setInfoType("'完成','延期'");
            }
            // 如果权限中包含运维用户
            if (roleKey.contains("om")) {
                message.setInfoType("'转单','延期','关闭'");
            }
        }
        // 设置接收用户名为当前登录用户
        message.setToName(getUsername());

        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    /**
     * 导出工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:message:export')")
    @Log(title = "工单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Message message)
    {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "工单信息数据");
    }

    /**
     * 获取工单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:message:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取工单信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(messageService.selectMessageById(id));
    }

    /**
     * 新增工单信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:message:add')")
    @Log(title = "工单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Message message)
    {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改工单信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:message:edit')")
    @Log(title = "工单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message)
    {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除工单信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:message:remove')")
    @Log(title = "工单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(messageService.deleteMessageByIds(ids));
    }
}
